//
//  ViesitorView.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/8.
//  Copyright (c) 2015年 name. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol ViesitorViewDelegate : NSObjectProtocol
{
   optional func register();

   func Login();
}

class ViesitorView: UIView {


    //外部接口
    
    func setImageAndTipText(imageName:String?,tip:String){
        tipLabel.text = tip
        
        if imageName == nil {
            cirecleViewRevolve()
            return
        }
        
        cirecleView.image = UIImage(named: imageName!)
        iconView.hidden = true
        
    }
    
    weak var ViesitorDelegate:ViesitorViewDelegate?
    
    @objc private func registerEvent(){
        if ViesitorDelegate != nil {
            ViesitorDelegate!.register?()
        }
    }
    
    @objc private func loginEvent(){
        if ViesitorDelegate != nil {
            ViesitorDelegate!.Login()
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
//        fatalError("init(coder:) has not been implemented")
        setupUI()
    }
   
    private func cirecleViewRevolve() {
        
     let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        anima.repeatCount = MAXFLOAT
        anima.toValue = 2 * M_PI
        anima.duration = 20
        //当动画结束 或者视图处于非活跃状态 动画不移除图层
        anima.removedOnCompletion = false
        
        cirecleView.layer .addAnimation(anima, forKey: nil)
    }
    
  
    private func setupUI() {
                
        addSubview(cirecleView)
        addSubview(bgView)
        addSubview(iconView)
        
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        cirecleView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
        }
         iconView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(cirecleView)
        }
        
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cirecleView)
            make.top.equalTo(cirecleView.snp_bottom).offset(16)
            make.width.equalTo(220)
            make.height.equalTo(40)
        }
        
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.left.equalTo(tipLabel)
            make.width.equalTo(90)
            make.height.equalTo(30)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
            make.right.equalTo(tipLabel)
            make.width.equalTo(90)
            make.height.equalTo(30)
        }
        
        bgView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(cirecleView)
            make.bottom.equalTo(loginBtn.snp_top).offset(-30)
            make.top.equalTo(cirecleView)
            make.left.right.equalTo(self)
        }
        
    }
    

    //MARK: 懒加载所有的控件
    
    private lazy var cirecleView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var bgView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var tipLabel:UILabel = UILabel(title: nil, Color: UIColor.lightGrayColor())
    
    
    private lazy var loginBtn: UIButton = {
        
        let btn = UIButton(title: "登录", bgImge: "common_button_white_disable", titleColor: UIColor.orangeColor())
        btn.addTarget(self, action:"loginEvent", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton(title: "注册", bgImge: "common_button_white_disable", titleColor: UIColor.orangeColor())
        
        btn.addTarget(self, action:"registerEvent", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
        
        }()
    

}
